package com.utils;

import com.model.WeightNode;

import java.text.DecimalFormat;
import java.util.*;

public class WeightUtils {



    public static List<WeightNode> buildTree(List<WeightNode> weightNodes ,int pid){
        List<WeightNode> treeList = new ArrayList<>();//构建pid节点的树
        for (WeightNode weightNode : weightNodes) {
            if(weightNode.getPid() == pid){
                weightNode.setChild(buildTree(weightNodes,weightNode.getId()));
                treeList.add(weightNode);
            }
        }
        return treeList;
    }

    public static double[] degreeArr(double [] points , double x){
        double[] arr = new double[4];
        Arrays.fill(arr,0);
        if(x<points[0]){
            arr[0] = 1;
        }else if(x < points[1]){
            arr[0] = (points[1] - x)/(points[1]-points[0]);
            arr[1] = (x-points[0])/(points[1] - points[0]);
            arr[2] = 0;
            arr[3] = 0;
        }else if(x< points[2]){
            arr[0] =0;
            arr[1] = 1;
            arr[2] =0;
            arr[3] = 0;
        }else if(x< points[3]){
            arr[0] = 0;
            arr[1] = (points[3] - x)/(points[3] - points[2]);
            arr[2] = (x- points[2])/(points[3] - points[2]);
            arr[3] = 0;
        }else if(x< points[4]){
            arr[0] = 0;
            arr[1] = 0;
            arr[2] = 1;
            arr[3] = 0;
        }else if(x < points[5]){
            arr[0] = 0;
            arr[1] =0;
            arr[2] = (points[5]-x)/(points[5]-points[4]);
            arr[3] = (x-points[4])/(points[5]-points[4]);
        }else{
            arr[3] = 1;
        }
        DecimalFormat decimalFormat = new DecimalFormat("#.000");
        arr[0] = Double.parseDouble(decimalFormat.format(arr[0]));
        arr[1] = Double.parseDouble(decimalFormat.format(arr[1]));
        arr[2] = Double.parseDouble(decimalFormat.format(arr[2]));
        arr[3] = Double.parseDouble(decimalFormat.format(arr[3]));

        return arr;
    }



    public static void printfEvaTree(WeightNode weightNode){
        WeightUtils.printfTree(weightNode,4);
    }

    /**
     * 根据评分区域数量打印评分数
     * @param weightNode
     * @param block
     */
    public static void  printfTree(WeightNode weightNode,int block ){
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < block; i++) {
            stringBuilder.append(" ");
        }
        stringBuilder.append(">");
        System.out.println(stringBuilder.toString() + weightNode);
        for (WeightNode node : weightNode.getChild()) {
            printfTree(node,block+5 );
        }
    }

    public static double [] product(double [] left ,double [][] right){
        DecimalFormat decimalFormat = new DecimalFormat("#.000");
        double []res = new double [4];
        for(int i = 0 ;i < 4 ;i++){
            for (int j = 0; j < left.length; j++) {
                   res[i] +=  left[j] * right[j][i];
            }
            res[i] = Double.parseDouble(decimalFormat.format(res[i]));
        }
        return res;
    }


    public static void main(String[] args) {

        // System.out.println(createInstance());
        // printfTree(createInstance(),0);

        // double[] doubles = degreeArr(BloodGlucose.FBG_BOUNDARY, 7.2);
        // System.out.println(Arrays.toString(doubles));
        double[] product = product(new double[]{0.75, 0.25}, new double[][]{{0.7, 0, 0.3, 0}, {0.64, 0.36, 0,0}});
        System.out.println(Arrays.toString(product));


    }


}
